const app = getApp();
Page({

    data: {
        key: '',
        order_id: '',
        order_goods: {},
        store_info: {},
        //        filename: {
        //            7141: {
        //                0: '2019/11/1047_06262701291622486.png'
        //            }
        //        },
        //        fileurl: {
        //            7141: {
        //                0: 'https://img.bdyoo.com/shop/member/1047/2019/11/1047_06262701291622486.png-wh240'
        //            }
        //        }
        goods_score: {},
        store_deliverycredit: 5
    },

    onLoad: function (options) {
        var self = this;
        self.setData({
            order_id: options.order_id
        })
        wx.getStorage({
            key: 'key',
            success: res => {
                self.setData({
                    key: res.data
                })
                self.getEvaluateInfo(res.data, options.order_id);
            }
        })

    },

    getEvaluateInfo: function (key, order_id) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_evaluate&op=index',
            data: {
                key: key,
                order_id: order_id
            },
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == 200) {
                    self.setData({
                        order_goods: res.data.datas.order_goods,
                        store_info: res.data.datas.store_info,
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            }
        })
    },
    chooseImage: function (e) {
        let index = e.currentTarget.dataset['index'];
        let rec_id = e.currentTarget.dataset['rec_id'];
        var self = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
            success: function (res) {

                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                //这里是上传操作
                wx.uploadFile({
                    url: app.globalData.ApiUrl + '/index.php?act=sns_album&op=file_upload', //里面填写你的上传图片服务器API接口的路径 
                    filePath: tempFilePaths[0], //要上传文件资源的路径 String类型 
                    name: 'file', //按个人情况修改，文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容，(后台接口规定的关于图片的请求参数)
                    header: {
                        "Content-Type": "multipart/form-data"//记得设置
                    },
                    formData: {
                        //和服务器约定的token, 一般也可以放在header中
                        key: self.data.key
                    },
                    success: function (res) {

                        //当调用uploadFile成功之后，再次调用后台修改的操作，这样才真正做了修改头像
                        if (res.statusCode = 200) {
                            var data = JSON.parse(res.data);
                            if (data.code = 200) {
                                self.imgSet(rec_id, index, data.datas.file_name, data.datas.file_url)
                                //                                self.setData({
                                //                                    filename: {
                                //                                        [rec_id]: {
                                //                                            [index]: data.datas.file_name
                                //                                        }
                                //                                    },
                                //                                    fileurl: {
                                //                                        [rec_id]: {
                                //                                            [index]: data.datas.file_url
                                //                                        }
                                //                                    }
                                //                                });
                            } else {
                                wx.showToast({
                                    title: '上传失败',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }

                        }
                    }
                })
            }
        })
    },
    imgSet: function (rec_id, index, file_name, file_url) {
        var self = this;
        var filename = self.data.filename;
        var file_name_obj = {};
        if (!filename[rec_id][index]) {
            file_name_obj.push = ({
                [index]: [file_name]
            });
        } else {
            file_name_obj = {
                [index]: [file_name]
            };
        }

        if (!filename[rec_id]) {
            filename.push({
                [rec_id]: [file_name_obj]
            });
        }

        self.setData({
            filename: filename
        });
    },
    chooseStar: function (e) {
        let index = e.currentTarget.dataset['index'];
        let rec_id = e.currentTarget.dataset['rec_id'];
        var self = this;
        self.setData({
            goods_score: {
                [rec_id]: [index]
            }
        });
    },
    chooseStarStore: function (e) {
        let index = e.currentTarget.dataset['index'];
        let type = e.currentTarget.dataset['type'];
        var self = this;
        self.setData({
            [type]: [index]
        });
    },
    formSubmit: function (e) {
        var self = this;
        var param = e.detail.value;
        param.key = self.data.key;
        param.order_id = self.data.order_id;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_evaluate&op=save',
            data: param,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
                if (res.data.code == 200) {
                    wx.redirectTo({
                        url: '../myOrder/myOrder?state_type=state_finnsh'
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        });
    }
})